package cn.lsnu.modules.login.controller;


import cn.lsnu.utils.Global;
import cn.lsnu.utils.RandomValidateCode;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;

@Controller
@RequestMapping("/login/weblogin")
public class ValidateCodeControler {


    @RequestMapping("/verifyCode")
    public void verfiyCode(HttpServletRequest request,
                           HttpSession session,
                           HttpServletResponse response,
                           Integer width,Integer height,Integer lineSize,Integer stringNum,Integer fontSize){
        RandomValidateCode randomValidateCode = new RandomValidateCode(width,height,lineSize,stringNum,fontSize);
        BufferedImage image = randomValidateCode.getRandCode();
        session.removeAttribute(Global.RANDOMCODEKEY);
        session.setAttribute(Global.RANDOMCODEKEY,randomValidateCode.getVerifyCode());
        try {
            ImageIO.write(image,"JPG",response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
